from skillchallenge.models import *
from skillchallenge.game import *
from django.contrib.auth.models import User

z = User.objects.create(username='e', email='e@e.com', password='e')
z.set_password('e')
z.save()


def viceversapath(loc1, loc2, vquest, cost):
    vquest.location_engine.create_path(loc1, loc2, cost)
    vquest.location_engine.create_path(loc2, loc1, cost)

quest = Quest()

Config.objects.create(max_skills=3)

perf = Skill.objects.create(name = 'Performance')
soc = Skill.objects.create(name = 'Social')
know = Skill.objects.create(name = 'Knowledge')

f = open('names', 'r')

for line in f.readlines():
    Name.objects.create(text = line.strip())

bertone = quest.location_engine.create_location('Bertone', 'a rural village settled over green hills.', True)
fugelestou = quest.location_engine.create_location('Fugelestou', 'a stronghold near a waterfall.', True)
helmeswella = quest.location_engine.create_location('Helmeswella', 'a walled city next a cliff.', True)
wroth = quest.location_engine.create_location('Wroth', 'a stronghold hidden in the forest.', True)
ascheby = quest.location_engine.create_location('Ascheby', 'a merchant city crossed by a river.')

viceversapath(bertone, fugelestou, quest, 5)
viceversapath(bertone, ascheby, quest, 5)
viceversapath(bertone, wroth, quest, 10)
viceversapath(ascheby, wroth, quest, 5)
viceversapath(ascheby, fugelestou, quest, 5)
viceversapath(ascheby, helmeswella, quest, 5)
viceversapath(fugelestou, helmeswella, quest, 5)
viceversapath(wroth, helmeswella, quest, 10)

ev1 = quest.event_engine.add_event(player_will=True, online=True,
                            preconditions = [quest.location_engine.is_in_precon(bertone)],
                            description = "look for a job.", cost = 1,
                            actions = [quest.code_engine.create_message_action('someone at the local tavern sent you to the Duke, who asked for a minstrel for a party.')])

ev2 = quest.event_engine.add_event(player_will=True, online=True,
                            preconditions = [quest.location_engine.is_in_precon(bertone)],
                            description = "accept the job of minstrel for the Duke of Bertone.", cost = 1,
                            actions = [quest.code_engine.create_message_action("at night you arrived at Duke's mansion, the guests fill up the hall and the Duke asked your for a song.")])

quest.chain(ev1, ev2)

bertonesminstrel = quest.new_quest("Duke Bertone's Minstrel: please the guests.", 100)

quest.turn_quest_acceptance(bertonesminstrel, ev2)

bminstrel_sc = quest.skill_challenge("pleasing Duke's guests", 2, 2)

ev3 = quest.event_engine.add_event(player_will=True, online=True,
                            preconditions = [quest.location_engine.is_in_precon(bertone)],
                            description = "play a song to the guests.", cost = 1,
                            actions = [quest.code_engine.create_message_action('you started to play an old song from your homeland.')])
quest.quest_event(bertonesminstrel, ev3)
bminstrel_play = quest.skill_test(ev3, perf, 10)
bminstrel_play.challenge = bminstrel_sc
bminstrel_play.save()

ev4 = quest.event_engine.add_event(online=True,
                            actions = [quest.code_engine.create_message_action('the guests loved your perfomance.')])
ev5 = quest.event_engine.add_event(online=True,
                            actions = [quest.code_engine.create_message_action('the guests hated your perfomance.')])

quest.chain_if_success(ev3, ev4, bminstrel_play)
quest.chain_if_fail(ev3, ev5, bminstrel_play)

ev6 = quest.event_engine.add_event(player_will=True, online=True,
                            preconditions = [quest.location_engine.is_in_precon(bertone)],
                            description = "entertain the guests.", cost = 1,
                            actions = [quest.code_engine.create_message_action('you talk amenities with the guests.')])
quest.quest_event(bertonesminstrel, ev6)
bminstrel_talk = quest.skill_test(ev6, soc, 10)
bminstrel_talk.challenge = bminstrel_sc
bminstrel_talk.save()

ev7 = quest.event_engine.add_event(online=True,
                            actions = [quest.code_engine.create_message_action('the guests enjoyed your conversation.')])
ev8 = quest.event_engine.add_event(online=True,
                            actions = [quest.code_engine.create_message_action('the guests was annoyed with your conversation.')])

quest.chain_if_success(ev6, ev7, bminstrel_talk)
quest.chain_if_fail(ev6, ev8, bminstrel_talk)



ev12 = quest.event_engine.add_event(online=True,
                                    actions = [quest.code_engine.create_message_action('your presence in the party was successful. The Duke is very pleased to you, and said that will offer you another opportunities if you want. Also members of the court of Fugelestou was very impressed with your perfomance and made an invitation to their hall.')])
quest.trigger_if_challenge_success(None, ev12, bminstrel_sc)

ev13 = quest.event_engine.add_event(online=True,
                                    actions = [quest.code_engine.create_message_action('your presence in the party was a failure. The Duke is disliked you, and asked you to leave his hall. But some members of the Fugelestou court said that you was not that bad and may find opportunity in their hall.')])
quest.trigger_if_challenge_fail(None, ev13, bminstrel_sc)

quest.successful_end(bertonesminstrel, ev12)
quest.failure_end(bertonesminstrel, ev13)